/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package almacen;

import static almacen.propiedades.code;
import static almacen.propiedades.openWebpage;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Tesla
 */
public class Almacen extends javax.swing.JFrame {
    
    public static int idUsuario = 1070;// es el usuario nulo

    Connection cn;
    PreparedStatement ps;
    ResultSet rs,rs2;
    ResultSetMetaData rsm;
    DefaultTableModel dtm;
    local_connection conexion;
    propiedades propprod;

    public void setUsuario(int user) {
        idUsuario = user;
    }      
    /**
     * Creates new form Almacen
     */
    public Almacen() {
        initComponents();
        dtm = (DefaultTableModel) this.jTable1.getModel();
        TableRowSorter<TableModel> elQueOrdena = new TableRowSorter<TableModel>(dtm);
        jTable1.setRowSorter(elQueOrdena);
        activo.setSelected(true);
        int[] anchos = {40, 80, 200, 50, 40, 50, 40, 50};
        jComboBox2.removeAllItems();
        jComboBox2.addItem("Por NOMBRE y DESCRIPCION, CODIGO");
        jComboBox2.addItem("Por CODIGO UBICACION");

        for (int i = 0; i < jTable1.getColumnCount(); i++) {
            jTable1.getColumnModel().getColumn(i).setPreferredWidth(anchos[i]);
        }
        
        conexion = new local_connection();
        try {
            cn = local_connection.getconnect();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        TableCellListener tcl = new TableCellListener(jTable1, action);
        consultarProductos();
    }
    AbstractAction action = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            TableCellListener tcl = (TableCellListener) e.getSource();
            String columna = null;
            switch (tcl.getColumn()) {
                case 1:
                    columna = "nombre";
                    break;
                case 2:
                    columna = "descripcion";
                    break;
                case 3:
                    columna = "codigo_ubicacion";
                    break;
                case 4:
                    columna = "cantidad_stock";
                    break;
                case 5:
                    columna = "ubicacionalmacen";
                    break;
                case 6:
                    columna = "cantidadalmacen";
                    break;
            }
            try {
                ps = (PreparedStatement) cn.prepareStatement("UPDATE productos SET " + columna + " = '" + String.valueOf(tcl.getNewValue()) + "' WHERE id_producto = "
                        + dtm.getValueAt(tcl.getRow(), 0));
                ps.execute();
                if(columna.equals("cantidadalmacen") || columna.equals("cantidad_stock")){
                    insertarRevision();                   
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }

        }
    };

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu2 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        checkWebsite = new javax.swing.JMenuItem();
        jComboBox2 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        activo = new javax.swing.JCheckBox();
        inventariadoCheck = new javax.swing.JCheckBox();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();

        jMenuItem1.setText("Propiedades");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem1);

        jMenuItem2.setText("Precios");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem2);

        jMenuItem3.setText("Transferir");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem3);

        jMenuItem4.setText("Stock");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem4);

        jMenuItem7.setText("Nuevo Lote");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem7);

        checkWebsite.setText("Ver Website");
        checkWebsite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkWebsiteActionPerformed(evt);
            }
        });
        jPopupMenu2.add(checkWebsite);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TeslaBEM Almacen");
        setIconImage(new ImageIcon(getClass().getResource("logo_rayito.png")).getImage());

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox2.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox2PopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Descripcion", "Ubic-M", "Cant-M", "Ubic-A", "Cant-A", "Fecha-Act"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setComponentPopupMenu(jPopupMenu2);
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(250);
        jTable1.getColumnModel().getColumn(3).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(4).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(5).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(6).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(7).setMinWidth(70);
        jTable1.getColumnModel().getColumn(7).setPreferredWidth(70);
        jTable1.getColumnModel().getColumn(7).setMaxWidth(70);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });

        activo.setText("Activo");
        activo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                activoActionPerformed(evt);
            }
        });

        inventariadoCheck.setText("Inventariado");
        inventariadoCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inventariadoCheckActionPerformed(evt);
            }
        });

        fileMenu.setMnemonic('f');
        fileMenu.setText("Configuracion");

        openMenuItem.setMnemonic('o');
        openMenuItem.setText("Cambiar Conexion");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("Salir");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jMenu1.setText("Productos");

        jMenuItem5.setText("Agregar Nuevo");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuItem6.setText("Productos bajos en stock");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        menuBar.add(jMenu1);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(activo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(inventariadoCheck))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(activo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inventariadoCheck))
        );

        getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox2PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBox2PopupMenuWillBecomeInvisible
        consultarProductos();
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2PopupMenuWillBecomeInvisible
    void consultarProductos() {
        
        dtm.setRowCount(0);
        String select;
        String consulta,activado="";
        String como, where1 = null, where2 = null, where3 = null;

        switch (jComboBox2.getSelectedIndex()) {
            case 1:
                where1 = "a.codigo_ubicacion LIKE '%";
                where2 = "%' OR a.ubicacionalmacen LIKE '%";
                where3 = "%' OR a.id_producto LIKE '%";
                break;
            default:
                where1 = "a.nombre LIKE '%";
                where2 = "%' OR a.descripcion LIKE '%";
                where3 = "%' OR a.id_producto LIKE '%";
                break;
        }
        if(activo.isSelected())activado=" AND activo = 1";
        try {
            //Class.forName("com.mysql.jdbc.Driver"); 
            //cn =   (Connection) DriverManager.getConnection("jdbc:mysql://localhost/tesla", "root", "tesla");             
            select = "SELECT a.id_producto, a.nombre, a.descripcion, a.codigo_ubicacion, a.cantidad_stock, a.ubicacionalmacen, a.cantidadalmacen, b.fecha FROM productos a LEFT OUTER JOIN revisiones b ON (a.id_producto = b.idProducto) WHERE (";

            como = jTextField1.getText();
            consulta = select + where1 + como + where2 + como + where3 + como + "%')"+activado;
            ps = (PreparedStatement) cn.prepareStatement(consulta);
            rs = ps.executeQuery();
            ArrayList<Object[]> data = new ArrayList<>();

            while (rs.next()) {
                Object[] rows = new Object[jTable1.getColumnCount()];
                for (int i = 0; i < rows.length; i++) {
                    rows[i] = rs.getObject(i + 1);
                }
                data.add(rows);
            }

            for (int i = 0; i < data.size(); i++) {
                dtm.addRow(data.get(i));
            }

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
    }
    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        consultarProductos();
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        int row = jTable1.getSelectedRow();
        if (row != -1) {
            try {
                propprod = new propiedades(Integer.parseInt(String.valueOf(jTable1.getValueAt(row, 0))));
                propprod.setLocationRelativeTo(null);
                propprod.setVisible(true);
            } catch (IOException ex) {
                Logger.getLogger(Almacen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
         jTable1.changeSelection(row, 0, false, false);
         jTable1.changeSelection(row, 1, false, false);
         jTable1.changeSelection(row, 2, false, false);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        int row = jTable1.getSelectedRow();
        if (row != -1) {
            Precios precio = new Precios(this, true, Integer.parseInt(String.valueOf(jTable1.getValueAt(row, 0))));
            precio.setLocationRelativeTo(null);
            precio.setVisible(true);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        int row = jTable1.getSelectedRow();
        int codigo = Integer.parseInt(String.valueOf(jTable1.getValueAt(row, 0)));
        if (row != -1) {
            Transferir transfiere = new Transferir(this, true, codigo);
            transfiere.setLocationRelativeTo(null);
            transfiere.setVisible(true);
        }
        consultarProductos();
                    
                jTable1.changeSelection(row, 0, false, false);
               jTable1.changeSelection(row, 1, false, false);
               jTable1.changeSelection(row, 2, false, false);
           
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        int row = jTable1.getSelectedRow();
        if (row != -1) {
            almacenado stock = new almacenado(Integer.parseInt(String.valueOf(jTable1.getValueAt(row, 0))));
            stock.setLocationRelativeTo(null);
            stock.setVisible(true);
        }
        consultarProductos();
         jTable1.changeSelection(row, 0, false, false);
               jTable1.changeSelection(row, 1, false, false);
               jTable1.changeSelection(row, 2, false, false);
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        cambiarConexion cambiarconexion = new cambiarConexion(this, true);
        cambiarconexion.setLocationRelativeTo(null);
        cambiarconexion.setVisible(true);
        try {
            cn = conexion.getconnect();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_openMenuItemActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        int row = jTable1.getSelectedRow();
        if (row != -1) {
            lotes lote = new lotes(this, true, Integer.parseInt(String.valueOf(jTable1.getValueAt(row, 0))));
            lote.setLocationRelativeTo(null);
            lote.setVisible(true);
        }
        consultarProductos();
         jTable1.changeSelection(row, 0, false, false);
               jTable1.changeSelection(row, 1, false, false);
               jTable1.changeSelection(row, 2, false, false);
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        editProducto nuevo = new editProducto();
        nuevo.setLocationRelativeTo(null);
        nuevo.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
Escasos bajos = new Escasos(this,true);
bajos.setLocationRelativeTo(null);
bajos.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void activoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_activoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_activoActionPerformed

    @SuppressWarnings("empty-statement")
    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        if(evt.getKeyCode()==116){
            insertarRevision();
        };
    }//GEN-LAST:event_jTable1KeyPressed

    private void inventariadoCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inventariadoCheckActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_inventariadoCheckActionPerformed

public static void openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(uri);
        } catch (Exception e) {
        }
    }
}

public static void openWebpage(URL url) {
    try {
        openWebpage(url.toURI());
    } catch (URISyntaxException e) {
    }
}
    
    private void checkWebsiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkWebsiteActionPerformed
        int productId = code;
        String sitio = "http://localhost";
        try {
            try {
                int row = jTable1.getSelectedRow();
                if (row != -1) {
                    productId = Integer.parseInt(String.valueOf(jTable1.getValueAt(row, 0)));
                }
                ps = (PreparedStatement)cn.prepareStatement("SELECT notas FROM productos WHERE id_producto=? LIMIT 1");
                ps.setInt(1,productId);
                rs = ps.executeQuery();
                if(rs.next()){
                    sitio = rs.getString(1);
                }
            } catch (SQLException ex) {
                Logger.getLogger(propiedades.class.getName()).log(Level.SEVERE, null, ex);
            }
            URL url = new URL(sitio);
            openWebpage(url);
        } catch (MalformedURLException ex) {
            Logger.getLogger(propiedades.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_checkWebsiteActionPerformed
    
    // Al modificar un stock en almacen o mostrador, se guarda un registro del movimiento. Esta funcion
    // Revisa si no existe un 
    
    private void insertarRevision(){
        if(inventariadoCheck.isSelected() == true){
            try {
                int id = (int) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0);
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                Date date = (Date) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 7);
                int almacen = (int) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 6);
                int stock = (int) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 4);
                if(date != null){
                    date = new Date();
                    ps = (PreparedStatement) cn.prepareStatement("UPDATE revisiones SET fecha = ? WHERE idProducto = "+id);
                    ps.setString(1,dateFormat.format(date));
                } else {
                    date = new Date();
                    ps = (PreparedStatement) cn.prepareStatement("INSERT INTO revisiones(idProducto,cantidadalmacen,cantidad_stock,fecha) VALUES(?,?,?,?)");
                    ps.setInt(1,id);
                    ps.setInt(2,almacen);
                    ps.setInt(3,stock);
                    ps.setString(4,dateFormat.format(date));
                }                
                ps.execute();
                consultarProductos();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activo;
    private javax.swing.JMenuItem checkWebsite;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JCheckBox inventariadoCheck;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    // End of variables declaration//GEN-END:variables
}
